package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellAnnounce;
import com.tsingtree.modular.entity.NanSeller;
import com.tsingtree.modular.entity.NanUser;
import com.tsingtree.modular.mapper.NanSellAnnounceMapper;
import com.tsingtree.modular.model.params.NanSellAnnounceParam;
import com.tsingtree.modular.model.result.NanSellAnnounceResult;
import com.tsingtree.modular.service.NanSellAnnounceService;
import com.tsingtree.modular.service.NanUserService;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 社区 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2024-03-08
 */
@Service
public class NanSellAnnounceServiceImpl extends ServiceImpl<NanSellAnnounceMapper, NanSellAnnounce> implements NanSellAnnounceService {

    @Autowired
    private NanUserService nanUserService;

    @Override
    public void add(NanSellAnnounceParam param){
        NanSellAnnounce entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellAnnounceParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellAnnounceParam param){
        NanSellAnnounce oldEntity = getOldEntity(param);
        NanSellAnnounce newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellAnnounceResult findBySpec(NanSellAnnounceParam param){
        return null;
    }

    @Override
    public List<NanSellAnnounceResult> findListBySpec(NanSellAnnounceParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellAnnounceParam param){
        Page pageContext = getPageContext();
        IPage<NanSellAnnounceResult> page = this.baseMapper.customPageList(pageContext, param);
        List<Integer> collect = page.getRecords().stream().map(NanSellAnnounceResult::getUserId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(collect)){
            List<NanUser> nanUsers = nanUserService.list(new QueryWrapper<NanUser>().in("id", collect));
            Map<Integer, String> map = nanUsers.stream().collect(Collectors.toMap(NanUser::getId, NanUser::getNickName));
            page.getRecords().forEach(result -> {
                result.setSellerName(map.get(result.getUserId()));
            });
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NanSellAnnounceParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellAnnounce getOldEntity(NanSellAnnounceParam param) {
        return this.getById(getKey(param));
    }

    private NanSellAnnounce getEntity(NanSellAnnounceParam param) {
        NanSellAnnounce entity = new NanSellAnnounce();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
